

$(function(){
	header();
	page_init();
	form_init();
	form_check();
});

// =======================================================================================================

function header()
{
	$(".header").addClass("hf-ellipsis").each(function(index, element) {
					
		elm = $(element).find(".pos-l");
		if( elm.length==1 )
		{
			elm.addClass("hf-ellipsis");
			
			if( elm.is(".icon") )
				$(element).addClass("icon-l1");
			else
				switch(elm.text().length)
				{
					case 0:
					case 1:
					case 2: $(element).addClass("word-l2"); break;
					case 3: $(element).addClass("word-l3"); break;
					case 4: $(element).addClass("word-l4"); break;
					default: $(element).addClass("word-l4");
				}
		}
		
		elm = $(element).find(".pos-r");
		if( elm.length==1 )
		{
			elm.addClass("hf-ellipsis");
			
			if( elm.is(".icon") )
				$(element).addClass("icon-r1");
			else
				switch(elm.text().length)
				{
					case 0:
					case 1:
					case 2: $(element).addClass("word-r2"); break;
					case 3: $(element).addClass("word-r3"); break;
					case 4: $(element).addClass("word-r4"); break;
					default: $(element).addClass("word-r4");
				}
		}
		
	});
}

function page_init()
{
	// 防误操作拖动
	$(document).on("dragstart",function(){return false;});
	
	// 列表视图
	$(".listview li a").each(function(index, element) {
        $(this).append("<img src='icons/listview_ar.png' class='icon enter' />");
    });
	
	// 侧边栏
	$("#set_username,#set_gender,#set_address,#set_descp").mmenu({classes:"mm-light mm-fullscreen",offCanvas:{position:"right",zposition:"front"}});
	
	$("#set_username .header .hd-return").click(function(){ $("#set_username").trigger("close.mm"); });
	$("#set_gender .header .hd-return").click(function(){ $("#set_gender").trigger("close.mm"); });
	$("#set_address .header .hd-return").click(function(){ $("#set_address").trigger("close.mm"); });
	$("#set_descp .header .hd-return").click(function(){ $("#set_descp").trigger("close.mm"); });
	
	$("#set_address .fm_address").mobiscroll().select({theme:"android-holo light",group:true});
}


function form_init()
{
	// form controls initial
	$(".single").wrap("<div class='group'><div class='bindgroup'></div></div>");
	$(".bindgroup").append($("<span class='tips'></span>"));
	
	$(".fm").show();
}


function form_check()
{
	
	// ===========================================[ 用户名 ]===================================================
	
	// 初始化默认样式
	$("input.fm_username").next().removeAttr("class").addClass("tips success");
	
	// 提交验证
	$("#set_username .submit").click(function(){
		
		old_data=$(".username").text();
		username=$("input.fm_username").val();
		switch(true)
		{
			case username=="":
				$("input.fm_username").focus().next().removeClass("success failed").addClass("warning");
				return false; break;
			case username==old_data:
				alert("用户名未做修改！");
				return false; break;
			default:
				$("input.fm_username").next().removeAttr("class").addClass("tips success");
		}
		
		// Ajax 提交数据
		$.ajax({
			type:"POST",
			url:"centent!set.do",
			data:{"key":"nickname","value":username},
			dataType:"json",
			success: function(result){

				rt=eval(result);

				status=rt["status"];
				if( status == "success!" )
					$("#set_username .submit").text("修改成功！");
				else
					$("#set_username .submit").text("修改失败！");
				
				setTimeout(function(){
					$(".username").text(username);
					$("#set_username").trigger("close.mm");
					$("#set_username .submit").text("确认修改！");
				},1000);
			}
		});
		
	});
	
	// 输入验证
	$("#set_username input.fm_username").keyup(function(e){
		
		username=$(this).val();
		switch(true)
		{
			case username=="":
				$(this).next().removeAttr("class").addClass("tips warning");
				return false; break;
			case !Boolean(username.match(/^[a-zA-Z\u4e00-\u9fa5]{1}[\u4e00-\u9fa5\w-]{0,18}[\u4e00-\u9fa5\w]{1}$/)):	// 正则：只能以汉字和字母开头，其余部分不能包含特殊字符
					$("input.fm_username").focus().next().removeAttr("class").addClass("tips failed");
					return false; break;
			default:
				$(this).next().removeAttr("class").addClass("tips success");
		}
		
		if(e.which==13)
		{
			$(this).blur();
			$("#set_username .submit").trigger("click");
		}
		
	});
	
	// ==============================================[ 性别 ]================================================
	
	// 提交验证
	$("#set_gender .submit").click(function(){
		
		old_data=$(".gender").text();
		gender=$("input[name=fm_gender]:checked").val();
		
		if( gender==old_data )
		{
			alert("性别未做修改！");
			return;
		}
		

		// Ajax 提交数据
		$.ajax({
			type:"POST",
			url:"centent!set.do",
			data:{"key":"gender","value":gender},
			dataType:"json",
			success: function(result){
				rt=eval(result);
				status=rt["status"];
				if( status == "success!" )
					$("#set_gender .submit").text("修改成功！");
				else
					$("#set_gender .submit").text("修改失败！");
				
				setTimeout(function(){
					if(gender=="0"){
						$(".gender").text("保密");
					}else if(gender=="1"){
						$(".gender").text("男");
					}else{
						$(".gender").text("女");
					}
				
					$("#set_gender").trigger("close.mm");
					$("#set_gender .submit").text("确认修改！");
				},1000);
			}
		});
		
	});
	
	// ==============================================[ 长居地 ]================================================
	
	// 提交验证
	$("#set_address .submit").click(function(){
		
		address=$(".fm_address").val();
		
		// Ajax 提交数据
		$.ajax({
			type:"POST",
			url:"centent!set.do",
			data:{"address":address},
			dataType:"json",
			success: function(result){
				rt=eval(result);
				status=rt["status"];
				if( status == "success!" )
					$("#set_address .submit").text("修改成功！");
				else
					$("#set_address .submit").text("修改失败！");
				
				setTimeout(function(){
					$(".address").text(gender);
					$("#set_address").trigger("close.mm");
					$("#set_address .submit").text("确认修改！");
				},1000);
			}
		});
		
	});
	
	// ==============================================[ 自我介绍 ]================================================
	
	// 提交验证
	$("#set_descp .submit").click(function(){
		
		descp=$(".fm_descp").val();
		$(".words").text(descp);
		// Ajax 提交数据
		$.ajax({
			type:"POST",
			url:"centent!set.do",
			data:{"key":"introduce","value":descp},
			dataType:"json",
			success: function(result){
				rt=eval(result);
				status=rt["status"];
				if( status == "success!" )
					$("#set_descp .submit").text("修改成功！");
				else
					$("#set_descp .submit").text("修改失败！");
				
				setTimeout(function(){
					$(".descp").text(descp);
					$("#set_descp").trigger("close.mm");
					$("#set_descp .submit").text("确认修改！");
				},1000);
			}
		});
		
	});
	
	
}
